package com.dick.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dick.base.aop.SmartQuery;
import com.dick.base.mapper.BaseNotifyMapper;
import com.dick.base.model.BaseNotify;
import com.dick.base.model.BusinessOrder;
import com.dick.base.model.ProduceItem;
import com.dick.base.util.DateUtil;
import com.dick.base.util.IdUtil;
import com.dick.base.util.PageInfo;
import com.dick.base.util.SmartQueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.List;

@Service
public class NotifyService {

    @Autowired
    private BaseNotifyMapper baseNotifyMapper;

    /**
     * 添加通知
     * @param notify
     */
    @Transactional(rollbackFor = Exception.class)
    public void addNotify(BaseNotify notify){
        notify.setId(IdUtil.nextId());
        notify.setNotifySendTime(DateUtil.now());
        notify.setNotifyRead(false);
        baseNotifyMapper.insert(notify);
    }

    /**
     * 分页查询通知
     * @return
     */
    @SmartQuery(clazz = BaseNotify.class)
    public Page<BaseNotify> notifyList(Long userId, PageInfo pageInfo){
        QueryWrapper<BaseNotify> queryWrapper = SmartQueryUtil.getQueryWrapper();
        queryWrapper.eq("user_id", userId);
        return baseNotifyMapper.selectPage(SmartQueryUtil.getPage(), queryWrapper);
    }

    /**
     * 设置通知已读
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void setNotifyRead(List<Long> ids){
        for (Long id : ids) {
            BaseNotify baseNotify = baseNotifyMapper.selectById(id);
            baseNotify.setNotifyRead(true);
            baseNotifyMapper.updateById(baseNotify);
        }
    }

    /**
     * 清空用户通知
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserNotify(Long userId){
        QueryWrapper<BaseNotify> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        baseNotifyMapper.delete(queryWrapper);
    }


}
